﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class LocalStorage
{
    public static bool HasKey(string key)
    {
        return PlayerPrefs.HasKey(key);
    } 
    public static bool SetItem(string key,string value)
    {
        if (string.IsNullOrEmpty(value)) return false;
        PlayerPrefs.SetString(key, CryptoUnit.EncryptByAES(value));
        return true;
    }
    public static string GetItem(string key,string defualt = "")
    {
        if(!PlayerPrefs.HasKey(key))return defualt;
        string value= PlayerPrefs.GetString(key);
        return CryptoUnit.DecryptByAES(value);
    }
    public static bool SetDoule(string key,double value)
    {
        string v = Convert.ToString(value);
        if (string.IsNullOrEmpty(v)) return false;
        return LocalStorage.SetItem(key, v);
    }
    public static double GetDoule(string key,double defualt=0)
    {
        return Convert.ToDouble(LocalStorage.GetItem(key, Convert.ToString(defualt)));
    }

    public static bool SetObject(string key,object node)
    {
        if(node == null) return false;
        return LocalStorage.SetItem(key,LitJson.JsonMapper.ToJson(node));
    }
    public static object GetObject(string key)
    {
        return LitJson.JsonMapper.ToObject(LocalStorage.GetItem(key, null));
    }

    public static bool RemoveItem(string key)
    {
        if(string.IsNullOrEmpty(key)) return false;
        if (PlayerPrefs.HasKey(key))
        {
            PlayerPrefs.DeleteKey(key);
            return true;
        }
        return false;
    }
    public static bool Clear()
    {
        PlayerPrefs.DeleteAll();
        return true;
    }
}
